import * as Router from 'koa-router';
import * as moment from 'moment';
import dataToLeaderService from '../services/dataToLeaderService';
import visitMonthPlanService from '../services/visitMonthPlanService';
import { IRouterContext } from 'koa-router';
import addNewService from '../services/addNewService';
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';


//领导查看员工的拜访统计数据
let router = new Router();
router.prefix('/api/leader');
//该接口是领导查看其管理的所有员工的统计数据列表---计划页面的统计数据
router.post('/listStatisticalData',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let json=await dataToLeaderService.checkAllUserDataByMonth(res.userid,res.month); 
    if(json !=null){
        ctx.body=json;
        ctx.status = 200;
    }else{
        ctx.body=[];
    };
    await next()
});

//该接口是移动端领导-月计划页面----模糊查询
// month:月份  character:模糊字段
router.post('/fuzzySearchStatisticsData',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let json=await dataToLeaderService.fuzzySearch(res.userid,res.character); 
    if(json.length>0){
        ctx.body=json;
        ctx.status = 200;
    }else{
        ctx.body=[];
    };
    await next()
});

//该接口是领导查看一个员工的今日计划---领导计划-今日计划页面(计划列表与拜访记录)
router.post('/listNowdayplanAndRecoderToLeader',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let json=await dataToLeaderService.showNowdayplanToLeader(res.userid); 
    if(json !=null){
        ctx.body=json;
        ctx.status = 200;
    }else{
        ctx.body={};
    };
    await next()
});

//该接口是向领导显示一个员工的月计划列表----领导计划-本月计划页面
router.post('/listMonthPlanToLeader',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let monthPlanList=await visitMonthPlanService.listMonthPlan2(res.userid,res.month);
    if(monthPlanList !=null){
        ctx.body=monthPlanList;
        ctx.status=200;
    }else{
        ctx.body={};
    } 
    await next()  
});


/* ==================下面是新增的接口,需要本地测试============================ */

//新增接口===>拜访频次的设定=====单次设定
//[{userId:'',code:'',number:0},{},{}]
router.post('/visitFrequencySet',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let b=await addNewService.getSetVisitFrequency(res.codelist);
    if(b>0){
        ctx.body='频次设定成功';
    }else{
        ctx.body='频次设定失败';
    } 
    ctx.status=200;
    await next()  
});
//拜访频次显示=======部门主管都能显示,销售不能显示
router.post('/visitFrequencyShow',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let directorArr=await addNewService.getDirectors('');
    let access_token=await dTalkApiUtil.getAccessToken();
    if(directorArr.includes(res.leaderId)){
        let data=await addNewService.showVsitFrequency(res.leaderId,access_token);
        if(data.length>0){
            ctx.body=data;
        }else{
            ctx.body=[]
        }
    }else{
        console.log('不是部门主管,无权限查看拜访频次数据')
        ctx.body=[]
    }
    await next()  
});

//判断员工是否可以继续新建拜访计划
router.post('/isBuildPlan',async(ctx: IRouterContext, next: any)=>{ 
    let rq:any=ctx.request;
    let res=rq.body;
    let json=await addNewService.isBuildPlan(res.userId,res.month,moment().format('YYYY'))
    ctx.body=json
    await next()  
});




export default router